#!/bin/sh                                                                         
# FILE:/usr/sbin/ifup-ppp                                                        
# Copyright (C) 2011 GuoWenxue <guowenxue@gmail.com>                              
# This file used to do PPP dial up.

#--------------------------------------------------                               
#     Function definition here                                                    
#--------------------------------------------------  
usage()
{
   prog=`basename $0`
   echo "$prog Usage: $prog [-d /dev/ttyUSB0] [-a apn] [-u username] [-p password] [-v chap/pap] [-i ipaddr] pppXX"
   echo ""
   echo " This shell script used to do pppd dial up on GPRS/3G modem, if no options gived, it"
   echo "will use the default configure file \"$network_cfg_dir/ifcfg-pppXX\". If get options"
   echo "in this command, it will use the option from the command line, and be careful the -d"
   echo "option must be given to specify the modem device."
   echo ""
   
   echo "pppXX: Required, specify the dial up generated PPP device, such as ppp10."
   echo "-d: Required, if use command line options, specify the modem TTY device."
   echo "-a: Option, APN(Access Point Name) for the SIM card provider."
   echo "-u: Option, username for the GPRS login if needed."
   echo "-p: Option, password for the GPRS login if needed."
   echo "-v: Option, authenticate method, pap or chap."
   echo "-i: Option, <local_IP_address>:<remote_IP_address> such as 0.0.0.0:0.0.0.0 "
   echo "Example: ifup-ppp -d /dev/ttyUSB0 -a 3gnet -u uid -p pwd ppp10"

   echo ""
   echo "Copyright (C) 2011 GuoWenxue <guowenxue@gmail.com>"
   exit
}

#===============================
#   Global variable declare    =
#===============================
modem=
inf=
apn=
uid=
pwd=


# Configure file
if [ -z "$network_cfg_dir" ];  then
   export network_cfg_dir=/apps/etc/network
fi 

#===============================
#    Shell body start here     =
#===============================

if [ $# -lt 1 ] ; then
   usage;
elif [ $# -eq 1 ] ; then
   inf=$1 
   conf=ifcfg-$inf
   
   cd $network_cfg_dir                                                          
   
   if [ ! -f $conf ]; then
     echo "================================================================================="
     echo "ERROR: $inf dial up configure file \"$network_cfg_dir/$conf\" not found!"
     echo "================================================================================="
     echo ""
     usage;
     exit;
   fi
   
   . $conf  
   modem=$MODEM
   apn=$APN
   uid=$USERNAME
   pwd=$PASSWORD
   vrfy=$VERIFY
   ipaddr=$IPADDR
   cd -
else
   while getopts "a:d:u:p:v:hi:" Option 
   do 
      case $Option in 
         d) modem=$OPTARG ;; 
         i) ipaddr=$OPTARG ;; 
         a) apn=$OPTARG ;; 
         u) uid=$OPTARG ;; 
         p) pwd=$OPTARG ;;
         v) vrfy=$OPTARG ;;
         h) pwd=$OPTARG;   usage ;; 
      esac 
   done
   shift $(($OPTIND - 1))
   inf=$*
fi

if [ -z "$modem" ] ; then
   usage;
   exit;
fi

if [ ! -c "$modem" ] ; then
   echo "ERROR: Modem device $modem doesn't exist!"
   exit
fi

inf=$( echo $inf | tr -cd '[0-9]\n' )


# Chat script need these two variable  
export APN=$apn
export DIALNUM="*99***1#"
chat_script="/etc/ppp/gprs-chat"

#======================
#  The pppd options   #
#======================

options="$modem 115200"

if [ -n "$uid" -a -n "pwd" ]; then
  options=$options" name $uid"
  options=$options" password $pwd"
fi

if [ -n "$inf" ] ; then
  options=$options" unit $inf"
fi

if [ "$vrfy" == "chap" ] ; then
   options=$options" require-chap"
else   
   options=$options" require-pap"
fi

# Restart after idle X second
options=$options" idle 86400"

options=$options" refuse-mschap refuse-mschap-v2 refuse-eap"

options=$options" nodefaultroute updetach"
options=$options" debug"

options=$options" lcp-echo-failure 3 lcp-echo-interval 5"
options=$options" ipcp-accept-local ipcp-accept-remote ipcp-restart 50"

options=$options" modem crtscts"


if [ -n "$ipaddr" ] ; then
  options=$options" $ipaddr"
fi

# Let the phone figure out all the IP addresses
options=$options" noipdefault"

options=$options" noauth"

# No ppp compression
options=$options" novj noccp novjccomp"

# For sanity, keep a lock on the serial line
options=$options" lock"

#=================================
#  PPP dial up command start up  #
#=================================
set -x

#chat option should can be: -v -E -V -f
pppd $options connect "/usr/sbin/chat -v -E -f $chat_script"

if [ -d /sys/class/net/ppp$inf ] ; then
   ip route add default dev ppp$inf metric 10
   #ip route add default dev ppp$inf
   /apps/tools/gatewayD > /dev/null 2>&1 
fi
